package ecommerce.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ecommerce.database.DataSource;
import ecommerce.model.Tag;

public class TagDAOImpl implements TagDAO {
	public List<Tag> getTags() {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection connection = null;
		List<Tag> tags = new ArrayList<Tag>();
		String sql = "SELECT * FROM tags;";
		try {
			connection = DataSource.getInstance().getConnection();
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				Tag t = new Tag();
				t.setTagId(rs.getInt(1));
				t.setTagDescription(rs.getString(2));
				tags.add(t);
			}
			ps.close();
			rs.close();
			connection.close();
			return tags;
		} catch(SQLException ex) {
			try { 
				ps.close();
				rs.close();
				connection.close();
			} catch (SQLException e) { }
			return tags;
		}
	}
	
	public Tag getTagByDescription(String tag_description) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection connection = null;
		Tag t = null;
		String sql = "SELECT * FROM tags WHERE tags.tag_description = ?;";
		try {
			connection = DataSource.getInstance().getConnection();
			ps = connection.prepareStatement(sql);
			ps.setString(1, tag_description);
			rs = ps.executeQuery();
			while(rs.next()) {
				t = new Tag();
				t.setTagId(rs.getInt(1));
				t.setTagDescription(rs.getString(2));
			}
			ps.close();
			rs.close();
			connection.close();
			return t;
		} catch(SQLException ex) {
			try { 
				ps.close();
				rs.close();
				connection.close();
			} catch (SQLException e) { }
			return t;
		}
	}
	
	public Tag getTagById(int id) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection connection = null;
		Tag t = null;
		String sql = "SELECT * FROM tags WHERE tags.tag_id = ?;";
		try {
			connection = DataSource.getInstance().getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next()) {
				t = new Tag();
				t.setTagId(rs.getInt(1));
				t.setTagDescription(rs.getString(2));
			}
			ps.close();
			rs.close();
			connection.close();
			return t;
		} catch(SQLException ex) {
			try { 
				ps.close();
				rs.close();
				connection.close();
			} catch (SQLException e) { }
			return t;
		}
	}
	
	public Tag addTag(String description) {
		PreparedStatement ps = null;
		Connection connection = null;
		String sql = "INSERT into tags(tag_description) VALUES(?);";
		try {
			connection = DataSource.getInstance().getConnection();
			ps = connection.prepareStatement(sql);
			ps.setString(1, description);
			ps.executeUpdate();
			ps.close();
			connection.close();
			return getTagByDescription(description);
		} catch(SQLException ex) {
			try { 
				ps.close();
				connection.close();
			} catch (SQLException e) { }
			return null;
		}
	}
}
